<?php


namespace App\Modules\Health\Http\Controllers\Api;


use App\Http\Controllers\Api\Controller;
use App\Modules\Health\Models\Disease;
use App\Modules\Health\Models\DiseaseClass;
use App\Modules\Health\Models\DiseaseScheme;
use App\Modules\Health\Models\HealthPlan;
use Illuminate\Http\Request;

class DiseaseController extends Controller
{
    public function index(Request $request)
    {
        $class = DiseaseClass::query()->select(['id', 'name'])->get();

        $class->first->load(['disease' => function($query){
            $query->select(['id','class_id','name','img'])->where('is_show', 1);
        }]);

        return $this->_success(compact('class'));
    }

    public function item(int $id)
    {
        $list = Disease::query()->select(['id', 'class_id', 'name', 'img'])
            ->where('class_id', '=', $id)
            ->where('is_show', 1)
            ->get();

        return $this->_success(compact('list'));
    }

    public function info(int $id)
    {
        $info = Disease::query()->find($id);
        if (!$info || !$info->is_show) {
            return $this->error('信息不存在');
        }

        $schemeList = DiseaseScheme::query()->where('disease_id', '=', $id)
            ->with('acu:id,name,img')
            ->orderBy('day')
            ->get();

        if ($schemeList) {
            $scheme = [];

            $schemeList = $schemeList->groupBy('day')->toArray();

            foreach ($schemeList as $day => $item) {
                $scheme[] = [
                    'day'  => sprintf('第%s天', $day),
                    'item' => array_map(function ($v) {
                        return [
                            'acu_info'  => sprintf('%s(%s)', $v['acu']['name'], $v['description']),
                            'acu_img'   => $v['acu']['img'],
                            'time_desc' => sprintf('各灸%d分钟', $v['time'])
                        ];
                    }, $item)
                ];
            }
        }

        $planId = request()->input('plan_id');
        $memberId = auth('api')->user()->id ?? 0;

        if ($memberId) {
            $plan = HealthPlan::where([
                'id'        => $planId,
                'member_id' => $memberId,
            ])->first();

            if ($plan && !$plan->status) {
                $lastFinishTime = strtotime(date('Y-m-d', $plan->last_finish_time));

                $planStatus = strtotime(date('Y-m-d')) == $lastFinishTime ? 2 : 1;
            }
        }


        $info->scheme = $scheme ?? [];
        $info->plan_status = $planStatus ?? 0;

        return $this->_success(compact('info'));
    }

    public function search(Request $request)
    {
        $keyword = $request->input('keyword');
        if (!$keyword) {
            return $this->error('关键词不能为空');
        }

        $keyword = cutKeyword($keyword);

        $selectRaw = 'CASE';

        foreach (explode('|', $keyword) ?? [] as $item) {
            $selectRaw .= sprintf(" WHEN name REGEXP '%s' THEN %d", $item, mb_strlen($item));
        }

        $selectRaw .= ' ELSE 0 END AS weight';

        $list = Disease::select(['id', 'class_id', 'name', 'img'])
            ->selectRaw($selectRaw)
            ->where('name', 'REGEXP', $keyword)
            ->where('is_show', 1)
            ->orderByDesc('weight')
            ->get();

        return $this->_success(compact('list'));
    }
}
